package in.muton.edushare.pages.group;

import java.util.List;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.images.ProfileImage;
import in.muton.edushare.helper.TinyMCEHelper;
import in.muton.edushare.pages.univ.UnivEdit;
import in.muton.edushare.pages.univ.UnivPage;
import in.muton.edushare.panels.upload.single.SingleUploadButtonPanel;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.url.URLLoader;

import org.apache.wicket.PageParameters;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.FileDirectoriesDAO;
import com.edushare.common.file.image.ProfileImages;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.FileDirectories;
import com.edushare.common.vo.FilesUploaded;

public class EditProfile extends GroupBase {

	public EditProfile(PageParameters params) {
		super(params);
		if(!getLoggedInUser().isGroupAdmin(super.groupModel.getObject())){
			throw new RestartResponseException(URLLoader.getPageClass(URLConstants.UNAUTHORIZED_PATH));
		}
		Form<Void> f = new Form<Void>("form");
		add(f);
		f.add(new TextField<String>("name", new PropertyModel<String>(this, "groupModel.object.groupName")));
		TextArea<String> ta = new TextArea<String>("desc", new PropertyModel<String>(this, "groupModel.object.description"));
		ta.add(TinyMCEHelper.getDefaultTinyMCEBehavior());
		f.add(ta);
		
		final ProfileImage pI = new ProfileImage("img", EditProfile.this.getGroupModel(), ApplicationConstants.FILE_SUFFIX_HBADGE);
		pI.setOutputMarkupId(true);
		f.add(pI);
		SingleUploadButtonPanel uploadButton = new SingleUploadButtonPanel
		
								("imgUpload", new PropertyModel<FileDirectories>(EditProfile.this, "groupGallery")) 
										{
											
											private static final long serialVersionUID = 2281037462277939285L;
								
											@Override
											protected void onUploadWindowClose(AjaxRequestTarget target) {
												List<FilesUploaded> lf = EduShareSession.get().getFileUploadedInLastUpload();
//												FilesUploaded fu = null;
												if(lf!=null && lf.size()>0){
													FilesUploaded fu = lf.get(lf.size()-1);//last item uploaded
													if(fu!=null){
														EditProfile.this.getGroupModel().getObject().setImageFile(fu);
														getSessionORMInterface().commitAllChanges();
													}
												}
												target.addComponent(pI);
											}
										};
		f.add(uploadButton);
		f.add(new Button("submit"){
			
			private static final long serialVersionUID = -8273424307820510119L;

			@Override
			public void onSubmit() {
				if(!StringUtil.isValid(groupModel.getObject().getGroupName())){
					setErrorMessage("Group name is required");
					return;
				}
				clearError();
				getSessionORMInterface().commitAllChanges();
				setResponsePage(GroupNews.class, EntityPageParams.getParams(groupModel));
			}
		});
		
	}

	@Override
	protected String getActivePage() {
		return "editProfile";
	}

	@Override
	public String getPageTitle() {
		return "Edit Group Profile | " + super.getGroupModel().getObject().getGroupName() + " | " + ApplicationConstants.APPLICATION_NAME;
	}
	
	public FileDirectories getGroupGallery(){
		FileDirectories fd = new FileDirectoriesDAO(getSessionORMInterface()).getOrCreateDefaultGallery(super.getGroupModel().getObject());
		fd.setFileProcessStrategy(new ProfileImages());
		return fd;
	}

}
